跳至主要内容

Bash / Shell 終端色彩與字型樣式

FreeBSD Archlinux Photon_OS Oracle_linux WSL Docker

參考資料

參考FLOZz’ MISC的說明頁面

1. 核心語法結構

控制碼的基本格式為:

\e[<樣式/顏色代碼>m
  • \e[(或 \033[\x1b[):CSI(Control Sequence Introducer)前綴。

  • <代碼>:樣式代碼,多個代碼可用分號 ; 串接(例如 \e[1;31;40m 代表粗體 + 紅字 + 黑底)。

  • m:Select Graphic Rendition(SGR)終止符號。

  • \e[0m重置控制碼。若最後沒有重置,後續整個終端的文字都會被染成該顏色!

基本使用範例

# 使用 echo (需加上 -e)
echo -e "\e[1;31m[ERROR]\e[0m 伺服器連線失敗"

# 更加推薦的 POSIX 標準寫法 (printf,免 -e 且各平台完全通用)
printf "\033[1;32m[SUCCESS]\033[0m 部署完成\n"

2. 文字樣式控制 (Text Formatting)

Formatting

Set

CodeDescriptionExamplePreview
1Bold/Brightecho -e "Normal \e[1mBold"Normal BoldNormal Bold
2Dimecho -e "Normal \e[2mDim"Normal DimNormal Dim
4Underlinedecho -e "Normal \e[4mUnderlined"Normal UnderlinedNormal Underlined
5Blink 1)echo -e "Normal \e[5mBlink"Normal BlinkNormal Blink
7Reverse (invert the foreground and background colors)echo -e "Normal \e[7minverted"Normal invertedNormal inverted
8Hidden (useful for passwords)echo -e "Normal \e[8mHidden"Normal HiddenNormal Hidden

Reset

CodeDescriptionExamplePreview
0Reset all attributesecho -e "\e[0mNormal Text"Normal TextNormal Text
21Reset bold/brightecho -e "Normal \e[1mBold \e[21mNormal"Normal Bold NormalNormal Bold Normal
22Reset dimecho -e "Normal \e[2mDim \e[22mNormal"Normal Dim NormalNormal Dim Normal
24Reset underlinedecho -e "Normal \e[4mUnderlined \e[24mNormal"Normal Underlined NormalNormal Underlined Normal
25Reset blinkecho -e "Normal \e[5mBlink \e[25mNormal"Normal Blink NormalNormal Blink Normal
27Reset reverseecho -e "Normal \e[7minverted \e[27mNormal"Normal inverted NormalNormal inverted Normal
28Reset hiddenecho -e "Normal \e[8mHidden \e[28mNormal"Normal Hidden NormalNormal Hidden Normal

8/16 colors

Foreground (text)

CodeColorExamplePreview
39Default foreground colorecho -e "Default \e[39mDefault"Default DefaultDefault Default
30Blackecho -e "Default \e[30mBlack"Default BlackDefault Black
31Redecho -e "Default \e[31mRed"Default RedDefault Red
32Greenecho -e "Default \e[32mGreen"Default GreenDefault Green
33Yellowecho -e "Default \e[33mYellow"Default YellowDefault Yellow
34Blueecho -e "Default \e[34mBlue"Default BlueDefault Blue
35Magentaecho -e "Default \e[35mMagenta"Default MagentaDefault Magenta
36Cyanecho -e "Default \e[36mCyan"Default CyanDefault Cyan
37Light grayecho -e "Default \e[37mLight gray"Default Light grayDefault Light gray
90Dark grayecho -e "Default \e[90mDark gray"Default Dark grayDefault Dark gray
91Light redecho -e "Default \e[91mLight red"Default Light redDefault Light red
92Light greenecho -e "Default \e[92mLight green"Default Light greenDefault Light green
93Light yellowecho -e "Default \e[93mLight yellow"Default Light yellowDefault Light yellow
94Light blueecho -e "Default \e[94mLight blue"Default Light blueDefault Light blue
95Light magentaecho -e "Default \e[95mLight magenta"Default Light magentaDefault Light magenta
96Light cyanecho -e "Default \e[96mLight cyan"Default Light cyanDefault Light cyan
97Whiteecho -e "Default \e[97mWhite"Default WhiteDefault White

Background

CodeColorExamplePreview
49Default background colorecho -e "Default \e[49mDefault"Default DefaultDefault Default
40Blackecho -e "Default \e[40mBlack"Default BlackDefault Black
41Redecho -e "Default \e[41mRed"Default RedDefault Red
42Greenecho -e "Default \e[42mGreen"Default GreenDefault Green
43Yellowecho -e "Default \e[43mYellow"Default YellowDefault Yellow
44Blueecho -e "Default \e[44mBlue"Default BlueDefault Blue
45Magentaecho -e "Default \e[45mMagenta"Default MagentaDefault Magenta
46Cyanecho -e "Default \e[46mCyan"Default CyanDefault Cyan
47Light grayecho -e "Default \e[47mLight gray"Default Light grayDefault Light gray
100Dark grayecho -e "Default \e[100mDark gray"Default Dark grayDefault Dark gray
101Light redecho -e "Default \e[101mLight red"Default Light redDefault Light red
102Light greenecho -e "Default \e[102mLight green"Default Light greenDefault Light green
103Light yellowecho -e "Default \e[103mLight yellow"Default Light yellowDefault Light yellow
104Light blueecho -e "Default \e[104mLight blue"Default Light blueDefault Light blue
105Light magentaecho -e "Default \e[105mLight magenta"Default Light magentaDefault Light magenta
106Light cyanecho -e "Default \e[106mLight cyan"Default Light cyanDefault Light cyan
107Whiteecho -e "Default \e[107mWhite"Default WhiteDefault White

4. 進階:256 色與 24-bit True Color (RGB)

現代終端機(如 iTerm2、Windows Terminal、Alacritty、Kitty)皆已支援更豐富的 256 色與 RGB 真彩色:

  • 256 色模式

    • 前景:\e[38;5;<0-255>m

    • 背景:\e[48;5;<0-255>m

    • 範例echo -e "\e[38;5;208m這是漂亮的橘色\e[0m"

  • 24-bit True Color (RGB)

    • 前景:\e[38;2;<R>;<G>;<B>m

    • 背景:\e[48;2;<R>;<G>;<B>m

    • 範例echo -e "\e[38;2;255;100;0m自定義 RGB 珊瑚色\e[0m"

5. 工程師實戰:封裝易讀的日誌函式 (Script Boilerplate)

不要在 Shell Script 內散落難懂的 \e[31m。業界標準做法是在腳本開頭統一定義常數變數或封裝 Helper Function:

#!/bin/bash

# --- 定義色彩變數 ---
C_RESET="\033[0m"
C_BOLD="\033[1m"
C_RED="\033[1;31m"
C_GREEN="\033[1;32m"
C_YELLOW="\033[1;33m"
C_BLUE="\033[1;34m"
C_CYAN="\033[1;36m"

# --- 日誌函式 ---
log_info() {
printf "${C_BLUE}[INFO]${C_RESET} %s\n" "$*"
}

log_success() {
printf "${C_GREEN}[SUCCESS]${C_RESET} %s\n" "$*"
}

log_warn() {
printf "${C_YELLOW}[WARN]${C_RESET} %s\n" "$*"
}

log_error() {
printf "${C_RED}[ERROR]${C_RESET} %s\n" "$*" >&2
}

# --- 測試呼叫 ---
log_info "正在啟動 Docker Compose 服務..."
log_warn "記憶體剩餘不足 512MB"
log_success "Nextcloud 備份成功完成"
log_error "無法連接 MariaDB 資料庫"